package com.example.springboot.MongoDB.BaseOperation;

import com.example.springboot.MongoDB.Vo.Person;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeleteFun {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 删除id 为1的记录
     */
    public void m1() {
        Query query = new Query(Criteria.where("id").is(1L));
        DeleteResult remove = mongoTemplate.remove(query, Person.class);
        System.out.println("删除的条数为：" + remove.getDeletedCount());//1

    }
    /**
     * 删除符合条件的所有文档
     */
    public void m2() throws Exception {
        //删除年龄小于18的所有人: lt 表示小于等于
        Query query = new Query(Criteria.where("age").lt(18));
        DeleteResult result = mongoTemplate.remove(query, Person.class);
        System.out.println("删除条数：" + result.getDeletedCount());
    }
    /**
     * 删除符合条件的单个文档 并返回删除的文档
     */
    public void m3() throws Exception {
        Query query = new Query(Criteria.where("id").is(2L));
        Person per = mongoTemplate.findAndRemove(query, Person.class);
        System.out.println(per);//Person(id=2, name=宋江, age=2)
    }
    /**
     * 删除符合条件的所有文档
     * 并返回删除的所有文档
     */
    public void m4(){
        // lt 表示小于
        Query query = new Query(Criteria.where("age").lt(3));
        List<Person> pers = mongoTemplate.findAllAndRemove(query, Person.class);
        System.out.println(pers);
    }
}